package com.gst.chat;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "MSG";
    private boolean isConnected = false;

    @Bind(R.id.et_login_name)
    EditText etLoginName;
    @Bind(R.id.et_login_pwd)
    EditText etLoginPwd;
    @Bind(R.id.bt_login_regist)
    Button btLoginRegist;
    @Bind(R.id.bt_login_login)
    Button btLoginLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_login_regist, R.id.bt_login_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_login_regist:
                ChatClient.getInstance().register(etLoginName.getText().toString().trim(),etLoginPwd.getText().toString().trim(), new Callback() {
                    @Override
                    public void onSuccess() {
                        Log.i(TAG, "creat_onSuccess: ");
                        runOnUiThread(new Runnable() {
                        @Override
                       public void run() {
                            Toast.makeText(LoginActivity.this, "已注册", Toast.LENGTH_SHORT).show();
                       }
                   });

                    }
                    @Override
                    public void onError(int errorCode, String s) {
                        Log.i(TAG, "creat_onError: " + s);
                        isConnected = false;
                    }

                    @Override
                    public void onProgress(int i, String s) {
                        Log.i(TAG, "creat_onProgress: " + s);
                    }
                });
                break;
            case R.id.bt_login_login:
//                if (ChatClient.getInstance().isLoggedInBefore()) {
////                    //已经登录 不能重复登录 不安全
////                    runOnUiThread(new Runnable() {
////                        @Override
////                        public void run() {
////                            Toast.makeText(LoginActivity.this, "你已在别处登录了，抱歉不能重复登录", Toast.LENGTH_SHORT).show();
////                        }
////                    });
//
//                } else {
                    //未登录，需要登录
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            ChatClient.getInstance().login(etLoginName.getText().toString().trim(),etLoginPwd.getText().toString().trim(), new Callback(){
                                @Override
                                public void onSuccess() {
                                    Log.e("tag", "login_onSuccess: ");
                                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                }

                                @Override
                                public void onError(int i, String s) {
                                    Log.i("tag", "login_error" + s);
                                }

                                @Override
                                public void onProgress(int i, String s) {

                                }
                            });
                        }
                    }).start();
//                }


                break;
        }
    }
}
